﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ChoViet.Lib.Common;
using ChoViet.Model;
using ChoViet.CMS.ViewModels;

namespace ChoViet.CMS.Controllers
{
	[HandleError]
    [Authorize()]
	public class HomeController : BaseController
	{
        ChoVietDataContext eDataContext = new ChoVietDataContext();

		public ActionResult About()
		{
			return View();
		}

        public ActionResult Index()
        {
            HomeViewModels model = new HomeViewModels();
            model.TotalVisited = eDataContext.Counters.Sum(e => e.Visited).Value;
            var y = eDataContext.Counters.Where(a => a.AddDate.Value.Day == DateTime.Now.AddDays(-1).Day && a.AddDate.Value.Month == DateTime.Now.Month && a.AddDate.Value.Year == DateTime.Now.Year).FirstOrDefault();
            var today = eDataContext.Counters.Where(e => e.AddDate.Value.Day == DateTime.Now.Day && e.AddDate.Value.Month == DateTime.Now.Month && e.AddDate.Value.Year == DateTime.Now.Year).FirstOrDefault();
            model.VisitToday = today != null ? today.Visited : 0;
            model.VisitYesterday = y != null ? y.Visited : 0;
            var contactToday = eDataContext.ContactUs.Count(e => e.CreatedDate.Value.Day == DateTime.Now.Day && e.CreatedDate.Value.Month == DateTime.Now.Month && e.CreatedDate.Value.Year == DateTime.Now.Year);
            var contactYesterday = eDataContext.ContactUs.Count(e => e.CreatedDate.Value.Day == DateTime.Now.AddDays(-1).Day && e.CreatedDate.Value.Month == DateTime.Now.Month && e.CreatedDate.Value.Year == DateTime.Now.Year);
            int contactTotalNumber = eDataContext.ContactUs.Count();
            model.ContactUsNumberToday = contactToday != null ? contactToday : 0;
            model.ContactUsNumberYesterday = contactYesterday != null ? contactYesterday : 0;
            model.TotalContactUsNumber = contactTotalNumber != null && contactTotalNumber > 0 ? contactTotalNumber : 0;
            var isAdmin = UserManager.CheckRole(RoleSystem.Administrator);
            return View(model);
        }

        public ActionResult Usermanage()
        {
            return View();
        }

        public ActionResult Introduction()
        {
            return View();
        }

        public ActionResult Regulations()
        {
            return View();
        }

        public ActionResult Postnews()
        {
            return View();
        }

        public ActionResult Feedback()
        {
            return View();
        }

        public ActionResult Contact()
        {
            return View();
        }

        [ChildActionOnly]
        public ActionResult Visited()
        {
            CounterViewModels c = new CounterViewModels();
            //c.TotalVisited = eDataContext.Counters.Sum(e => e.Visited).Value;
            //var y = eDataContext.Counters.Where(a => a.AddDate.Value.Day == DateTime.Now.AddDays(-1).Day &&
            //        a.AddDate.Value.Month == DateTime.Now.Month &&
            //        a.AddDate.Value.Year == DateTime.Now.Year).FirstOrDefault();
            //c.Yesterday = y != null ? y.Visited : 0;
            return View(c);
        }

	}
}
